package com.ruoyi.project.api.user;


import com.ruoyi.common.utils.IpUtils;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.system.config.service.IConfigService;
import com.ruoyi.project.user.entity.LoginByMobileReq;
import com.ruoyi.project.user.service.ITUserService;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ObjectUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 用户端登陆验证
 *
 */
@RestController
@RequestMapping("/api/user")
public class WebUserController extends BaseController {

    @Autowired
    ITUserService userService;
    @Autowired
    private IConfigService configService;

    private static final Logger log = LoggerFactory.getLogger(WebUserController.class);

    @ApiOperation("登录")
    @Log(title = "APP接口管理", businessType = BusinessType.OTHER)
    @PostMapping("/login")
    public AjaxResult loginByMobile (LoginByMobileReq req, HttpServletRequest request) {
        try {
            if(ObjectUtils.isEmpty(req)){
                return AjaxResult.error("信息有误,请检查");
            }
            req.setLastLoginIp(IpUtils.getIpAddr(request));
            Map<String, Object> map = this.userService.loginByMobile(req);
            return AjaxResult.success(map);
        } catch (Exception e) {
            return this.error("登录失败 " + e.getMessage());
        }
    }

    /**
     * 小程序获取openId
     *
     * @return
     */
    @ApiOperation("小程序获取openId")
    @Log(title = "APP接口管理", businessType = BusinessType.OTHER)
    @GetMapping("/getWXAPPCode2Session")
    public AjaxResult  getWXAPPCode2Session (String code) {
        if(ObjectUtils.isEmpty(code)){
            return AjaxResult.error("code不能为空,请检查");
        }
      String wxAppid=  configService.selectConfigByKey("WX_APPID");
        if(ObjectUtils.isEmpty(wxAppid)){
            return AjaxResult.error("配置异常,请检查");
        }
       String wxSecret= configService.selectConfigByKey("WX_SECRET");
        if(ObjectUtils.isEmpty(wxSecret)){
            return AjaxResult.error("配置异常,请检查");
        }
        //没有则请求获取token
        Map <String, String> param = new HashMap <String, String>();
        param.put("appid", wxAppid);
        param.put("secret", wxSecret);
 /*       param.put("appid", "wx58cd94250038dbdb");
        param.put("secret", this.secret);*/
        param.put("js_code", code);
        param.put("grant_type", "authorization_code");
        //发送get请求
        String result = HttpUtils.sendGet("https://api.weixin.qq.com/sns/jscode2session", HttpUtils.getParam(param));
        log.info("测试返回数据" + result);
        return AjaxResult.success("成功",result);
    }



}
